package com.sean.common.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import android.os.Environment;

public class Log {
	
	private final static boolean loggable = true;
	private final static String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
	
	public static void d(String tag, String msg){
		if(loggable){
			android.util.Log.d(tag, msg);
		}
	}
	
	public static void i(String tag, String msg){
		if(loggable){
			android.util.Log.i(tag, msg);
		}
	}
	
	public static void e(String tag, String msg){
		if(loggable){
			android.util.Log.e(tag, msg);
		}
	}
	
	public static void e(String tag, String msg, Exception e){
		if(loggable){
			android.util.Log.e(tag, msg, e);
		}
	}
	
	public static void toFile(String tag,String fileName,String msg){
		if(loggable){
			try {
				long position = 0;
				File file = new File(SDCARD+fileName);
				if(!file.exists()){
					file.createNewFile();
				}else{
					position = file.length();
				}
				RandomAccessFile raf = new RandomAccessFile(file, "rw");
				raf.seek(position);
				raf.write(msg.getBytes("UTF-8"));
				raf.write("\n".getBytes());
				raf.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
